package com.shure.queue.redisMqPc.conifg;

import com.shure.queue.redisMqPc.ConsumerContainer;
import com.shure.queue.redisMqPc.RedisQueue;
import com.shure.queue.redisMqPc.annotation.PcConsumer;
import com.shure.queue.redisMqPc.consumer.Consumer;
import com.shure.queue.utils.ApplicationContextUtils;
import org.reflections.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;
import java.util.Set;

@Configuration
public class RedisMqConfig {
    /**
     * 配置消息监听
     *
     * @return 消费者容器
     */
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public ConsumerContainer redisMqConsumerContainer(@Autowired RedisTemplate<String, Object> redisTemplate) {
        ConsumerContainer config = new ConsumerContainer(redisTemplate);

        Map<String, Object> beans = ApplicationContextUtils.getBeansWithAnnotation(PcConsumer.class);
        for (Map.Entry<String, Object> entry : beans.entrySet()) {
            Consumer consumer = (Consumer) entry.getValue();
            PcConsumer pcConsumer = AnnotationUtils.findAnnotation(consumer.getClass(), PcConsumer.class);
            config.addConsumer(RedisQueue.builder().queue(pcConsumer.key()).consumer(consumer).build());
        }
        return config;
    }

}
